﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;

namespace Fighter.Fights.ModuleData
{

    internal class RadarMatchParam : RadarParameters
    {
        public RadarMatchParam(int ID)
        {
            Params = new List<object>();
            Params.Add(ID);
        }
    }


    #region Radar Match

    internal class RadarMatch : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;

        public RadarMatch()
            : base()
        {
            rdrCodeColumn = FG_Fight.ID.Name;
            rdrDescColumn = FG_Fight.Description.Name;
            rdrNameSpace = "Fighter.Fighter.Accounting.Forms.Match";
        }

        protected override Table GetRadarTable()
        {
            return new FG_Fight();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            qb.Clear();
            qb.AddSelectAll(FG_Fight.Name);
            qb.AddFrom(FG_Fight.Name);
            qb.AddCompare(FG_Fight.ID, "@p1");

            p1 = new SqlABParameter("@p1", FG_Fight.ID);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[FG_Fight.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            qb.Clear();
            qb.AddSelectAll(FG_Fight.Name);
            qb.AddFrom(FG_Fight.Name);
            qb.AddFilter(findQuery);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarMatchParam(DataGridViewRowValues.GetValue<int>(row, FG_Fight.ID));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarMatchParam(int.Parse(text));
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion
}